<?php

namespace Summer\Hjd\Http\Controllers;

use Illuminate\Routing\Controller;
use RainLab\User\Models\User as UserModel;
use RLuders\JWTAuth\Classes\JWTAuth;
use Summer\Hjd\Models\ShareModel;
use Summer\Hub\Http\Controllers\Traits\ApiReturn;
use Input;
use Config;
use Db;
use Exception;
use Summer\Shop\Models\ProductModel;


class ShareController extends Controller
{
    use ApiReturn;

    /**
     * 创建分享记录
     * @param JWTAuth $auth
     */
    public function create(JWTAuth $auth){
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $spm=Input::get('spm');
        if(!$spm){
            return $this->apiError('参数错误');
        }
        $params=explode('.',$spm);
        $share_uid=$params[0];
        if($share_uid==$user->id){
            return $this->apiError('不能分享给本人');
        }
        $type_id=$params[2];
        switch ($params[1]){
            case 1:
                $type='index';
                $msg="商城";
                break;
            case 2:
                $type='goods';
                $product=ProductModel::where('id',$type_id)->first();
                $msg="商品".$product->name;
                break;
            default:
                $type='';
        }
        $from='forward';
        switch ($params[3]){
            case 1:
                $share_platform='H5';
                break;
            case 2:
                $share_platform='wxOfficialAccount';
                break;
            case 3:
                $share_platform='wxMiniProgram';
                break;
            case 4:
                $share_platform='App';
                break;
            default:
                $type='';
        }
        $platform=$share_platform;
        $user_id=$user->id;
        $remark="通过您的分享转发访问了".$msg;
        //增加分享记录
        $share_table=new ShareModel();
        $share_table->from=$from;//不知道什么意思，预留
        $share_table->remark=$remark;//备注
        $share_table->platform=$platform;//备注
        $share_table->user_id=$user_id;//备注
        $share_table->share_platform=$share_platform;//备注
        $share_table->share_uid=$share_uid;//备注
        $share_table->type=$type;//备注
        $share_table->type_id=$type_id;//备注
        $share_table->save();
        //确定推荐关系
        $share_user=UserModel::where('id',$share_uid)->first();
        //在这里判断是否要设置推荐关系
        $set_tuijian=1;
        $user_count=UserModel::where('tuid',$user->id)->count();
        //有值的话就跳过。自己已经推荐有人的话，自己就不能再绑定人了
        if($user_count>0){
            $set_tuijian=0;
        }
        //自己有推荐人了，不能绑定
        if($user->tuid>0){
            $set_tuijian=0;
        }
        if($set_tuijian==1){
            $user->tuid=$share_user->id;
            $user->tids=$share_user->tids.$share_user->id.",";
            $user->tdeep=$share_user->tdeep+1;
            $user->save();
        }
        return $this->apiSuccess('操作成功');
    }

    /**
     * 分享记录
     */
    public function index(JWTAuth $auth)
    {
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $page=Input::get('page',1);
        $page_size=Input::get('page_size',20);
        $query=ShareModel::select();
        $query->with([
            'relate'=>function($q){
                $q->with(['avatar']);
            }
        ]);
        if(Input::has('type')){
            $type=Input::get('type');
            if($type!='all'){
                $query->where('type',$type);
            }
        }
        $query->where('share_uid',$user->id);
        $query->orderBy('id','DESC');
        $records=$query->paginate($page_size,$page);
        $records->each(function ($record){
            if($record->type=='goods'){
                $product=ProductModel::where('id',$record->type_id)->first();
                $array=['image'=>$product->image_preview->path];
                $record->type_data=$array;
            }elseif($record->type=='index'){
                $record->type_data=[];
            }
        });
        return $this->apiSuccess('操作成功',$records);
    }
}
